/*
Joseph Ratway
Garrett Brown
Bret Parker
*/

//imports
import java.io.*;
import java.util.*;


public class Parser
{
	String command;
	int undoCount=0;
	Scanner user_input = new Scanner( System.in);
	int state = 0;
	

	Parser(){};

	int ParseLine(ReversiGame rgame)
	{
		System.out.print("Enter your Command or Move:");
		command = user_input.next();
		
		if(checkCommandSyntax(command)==true)
		{
			if(command.compareTo("REDO")==0)
			{
				rgame.RedoMove();
				return 3;
			}
			else if(command.compareTo("UNDO")==0)
			{
				rgame.UndoMove();
				undoCount++;
				return 4;
			}
			
			else if(command.compareTo("SHOW_NEXT_POS")==0)
			{
			
				rgame.currentBoard.isValidMove(rgame.currentBoard.currentBoard);
				rgame.currentBoard.printBoard();
				ParseLine(rgame);
				return 1;
				
			}
			else if(command.compareTo("EXIT")==0)
			{
				System.exit(0);
				return 1;
			}
			else if(command.compareTo("DISPLAY_ON")==0)
			{
				//display on
				return 1;
			}
			else if(command.compareTo("DISPLAY_OFF")==0)
			{
				//display off
				return 1;
			}
			else if(command.compareTo("EASY")==0)
			{
				//easy
				return 1;
			}
			else if(command.compareTo("RANDOM")==0)
			{
				//Random
			
			
				return 1;
			}
			else if(command.compareTo("GREEDY")==0)
			{
				//easy
				
				
				return 1;
			}
			else if(command.compareTo("MEDIUM")==0)
			{
				//medium
				return 1;
			}
			else if(command.compareTo("HARD")==0)
			{
				//hard
				return 1;
			}
			else if(command.compareTo("BLACK")==0)
			{
				//black
				return 1;
			}
			else if(command.compareTo("WHITE")==0)
			{
				//white
				return 1;
			}
			else if(command.compareTo("ran")==0)
			{
				rgame.currentBoard.random(rgame.currentBoard.currentBoard);
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
		else if(checkMoveSyntax(command)==true)
		{ 
			int i=1;
			while(undoCount>=0)
				{
					rgame.boardStates.remove(rgame.boardStates.size()-i);
					undoCount--;
					i++;
				}
				undoCount=0;
			if(rgame.currentBoard.endCondition>=0)
			{
				rgame.currentBoard.makeMove(rgame.currentBoard.currentBoard, rgame.currentBoard.player, command);
				state=0;
			}
			return 1;
		}
		else
	    {
			System.out.println("Incorrect Input, Try Again");
			ParseLine(rgame);
		}
		
		return 0;
	}
	
	boolean checkCommandSyntax(String str)
	{	
	
		if( (str.compareTo("GREEDY")==0) ||(str.compareTo("RANDOM")==0) ||(str.compareTo("REDO")==0) ||(str.compareTo("UNDO")==0) || (str.compareTo("SHOW_NEXT_POS")==0) || (str.compareTo("EXIT")==0)|| (str.compareTo("DISPLAY_ON")==0) || (str.compareTo("DISPLAY_OFF")==0)|| (str.compareTo("EASY")==0) || (str.compareTo("MEDIUM")==0)|| (str.compareTo("HARD")==0) || (str.compareTo("BLACK")==0) || (str.compareTo("WHITE")==0)||(str.compareTo("ran")==0)) 
			return true;
		else
			return false;
				
	}
	
	boolean checkMoveSyntax(String str)
	{
	
		char[] charArray;
		charArray = str.toCharArray();
        if(str.length() < 2)
        {
            return false;
        }
	
		if((charArray[0] >= 'a' && charArray[0] <= 'z'))
		{
			if(Character.isDigit(charArray[1]) == true )
				return true;
			else
				return false;
		}
		else
			return false;	
	}
	
}
	
	
	
